package com.wtb.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wtb.dao.RrDao;
import com.wtb.pub.WtbException;
import com.wtb.qq.domain.QqOauth;
import com.wtb.rr.domain.RrDetailInfo;
import com.wtb.rr.domain.RrOauth;
import com.wtb.utils.JdbcUtils;

public class RrDaoImpl implements RrDao {
	private static final Log log = LogFactory.getLog(RrDaoImpl.class);
	public void insert(RrOauth rrOauth) {
		try {
			QueryRunner qr = new QueryRunner();
			String sql = "insert into rrdata(uni,access_token,refresh_token,expires_in,createTime,ref,r2q) values (?,?,?,?,?,?,?)";
			Object[] params = {rrOauth.getUni(),rrOauth.getAccess_token(),rrOauth.getRefresh_token(),rrOauth.getExpires_in(),rrOauth.getCreateTime(),rrOauth.getRef(),rrOauth.getR2q()};
			qr.update(JdbcUtils.getConnection(), sql, params);
		} catch (SQLException e) {
			log.error("插入rrdata错误：", e);
			throw new WtbException("插入rrdata错误"+e);
		}

	}
	public void insert(RrDetailInfo rrDetailInfo) {
		try {
			QueryRunner qr = new QueryRunner();
			String sql = "insert into rrdata(uni,access_token,refresh_token,expires_in,createTime,ref,r2q,r2s,name,sex,hometown,university) values (?,?,?,?,?,?,?,?,?,?,?,?)";
			Object[] params = {rrDetailInfo.getUni(),rrDetailInfo.getAccess_token(),rrDetailInfo.getRefresh_token(),rrDetailInfo.getExpires_in(),rrDetailInfo.getCreateTime(),rrDetailInfo.getRef(),rrDetailInfo.getR2q(),rrDetailInfo.getR2s(),rrDetailInfo.getName(),rrDetailInfo.getSex(),rrDetailInfo.getHometown(),rrDetailInfo.getUniversity()};
			qr.update(JdbcUtils.getConnection(), sql, params);
		} catch (SQLException e) {
			log.error("插入rrdata错误：", e);
			throw new WtbException("插入rrdata错误"+e);
		}

		
	}

	
	public void del(RrOauth rrOauth) {
		try {
			QueryRunner qr = new QueryRunner();
			String sql = "delete from rrdata where uni=?";
			qr.update(JdbcUtils.getConnection(), sql, rrOauth.getUni());
		} catch (SQLException e) {
			log.error("删除rrdata错误：", e);
			throw new WtbException("删除rrdata错误"+e);
		}
	}
	
	public void update(RrOauth rrOauth) {
		try {
			QueryRunner qr = new QueryRunner();
			String sql = "update rrdata set access_token=?,refresh_token=?,expires_in=?,createTime=?,ref=?,r2q=? where uni=?";
			Object[] params = {rrOauth.getAccess_token(),rrOauth.getRefresh_token(),rrOauth.getExpires_in(),rrOauth.getCreateTime(),rrOauth.getRef(),rrOauth.getR2q(),rrOauth.getUni()};
			qr.update(JdbcUtils.getConnection(), sql, params);
		} catch (SQLException e) {
			log.error("更新rrdata错误：", e);
			throw new WtbException("更新rrdata错误"+e);
		}

	}
	
	public void update(RrDetailInfo rrDetailInfo) {
		try {
			QueryRunner qr = new QueryRunner();
			String sql = "update rrdata set name=?,sex=?,hometown=?,university=? where uni=?";
			Object[] params = {rrDetailInfo.getName(),rrDetailInfo.getSex(),rrDetailInfo.getHometown(),rrDetailInfo.getUniversity(),rrDetailInfo.getUni()};
			qr.update(JdbcUtils.getConnection(), sql, params);
		} catch (SQLException e) {
			log.error("更新rrdata错误：", e);
			throw new WtbException("更新rrdata错误"+e);
		}
		
	}

	@SuppressWarnings("deprecation")
	public RrOauth findByQq(QqOauth qqOauth) {
		try {
			QueryRunner qr = new QueryRunner();
			String sql = "select * from rrdata where ref=?";
			RrOauth rrOauth = (RrOauth) qr.query(JdbcUtils.getConnection(), sql,qqOauth.getUni(),new BeanHandler(RrOauth.class));
			return rrOauth;
		} catch (SQLException e) {
			log.error("通过qqoauth查找rrdata错误：", e);
			throw new WtbException("通过qqoauth查找rrdata错误"+e);
		}
	}

	@SuppressWarnings("deprecation")
	public RrOauth findByRr(RrOauth rrOauth) {
		try {
			QueryRunner qr = new QueryRunner();
			String sql = "select * from rrdata where uni=?";
			rrOauth = (RrOauth) qr.query(JdbcUtils.getConnection(), sql,rrOauth.getUni(),new BeanHandler(RrOauth.class));
			return rrOauth;
		} catch (SQLException e) {
			log.error("通过rroauth查找rrdata错误：", e);
			throw new WtbException("通过rroauth查找rrdata错误"+e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<RrOauth> getAll() {
		try {
			QueryRunner qr = new QueryRunner();
			String sql = "select * from rrdata";
			return (List<RrOauth>) qr.query(JdbcUtils.getConnection(), sql,new BeanListHandler(RrOauth.class));
		} catch (SQLException e) {
			log.error("查询rrdata所有信息：", e);
			throw new WtbException("查询rrdata所有信息"+e);
		}
	}
	@SuppressWarnings("unchecked")
	public List<RrDetailInfo> getAllInfo() {
		try {
			QueryRunner qr = new QueryRunner();
			String sql = "select * from rrdata order by createTime DESC";
			return (List<RrDetailInfo>) qr.query(JdbcUtils.getConnection(), sql,new BeanListHandler(RrDetailInfo.class));
		} catch (SQLException e) {
			log.error("查询rrdata所有信息：", e);
			throw new WtbException("查询rrdata所有信息"+e);
		}
	}


}
